//
//  SFCTestAuthProvider.m
//  SVNForCocoa
//
//  Created by Jeremy Pereira on 25/11/2013.
//  Copyright (c) 2013 Jeremy Pereira. All rights reserved.
//

#import "SFCTestAuthProvider.h"
#import "SFCCredentials.h"
#import "SFCError.h"
#import <svn_error_codes.h>
#import <svn_auth.h>

@implementation SFCTestAuthProvider

-(NSString*) kind
{
    @throw [NSException exceptionWithName: [self className]
                                   reason: [NSString stringWithFormat: @"%s not implemented", __PRETTY_FUNCTION__]
                                 userInfo: nil];
}

-(id) makeIterator
{
    return nil;
}

-(SFCCredentials*) credentialsForRealm: (NSString*) realm
                            parameters: (NSDictionary*) parameters
                              iterator: (id) iterator
                             firstTime: (bool) isFirstTime
                                 error: (NSError *__autoreleasing*) error
{
    @throw [NSException exceptionWithName: [self className]
                                   reason: [NSString stringWithFormat: @"%s not implemented", __PRETTY_FUNCTION__]
                                 userInfo: nil];
}

-(bool) saveCredentials: (SFCCredentials*) credentials
               forRealm: (NSString*) realm
             parameters: (NSDictionary*) parameters
                  error: (NSError *__autoreleasing *)error
{
    return true;
}

@end

@implementation SFCTestUserNameProvider


-(NSString*) kind
{
    return [NSString stringWithUTF8String: SVN_AUTH_CRED_USERNAME];
}

-(id) makeIterator
{
    return nil;
}

-(SFCCredentials*) credentialsForRealm: (NSString*) realm
                            parameters: (NSDictionary*) parameters
                              iterator: (id) iterator
                             firstTime: (bool) isFirstTime
                                 error: (NSError *__autoreleasing*) error
{
    NSString* userName = NSUserName();
    SFCUserNameCredentials* userNameCredentials
    	= [[SFCUserNameCredentials alloc] initWithName: userName maySave: false];
    return userNameCredentials;
}

-(bool) saveCredentials: (SFCCredentials*) credentials
               forRealm: (NSString*) realm
             parameters: (NSDictionary*) parameters
                  error: (NSError *__autoreleasing *)error
{
    return true;
}

@end

@implementation SFCTestSimpleProvider
{
    NSString* _password;
}

-(id) init
{
	return [self initWithPassword: @""];
}

-(id) initWithPassword: (NSString*) password
{
    self = [super init];
    if (self != nil)
    {
        _password = password;
    }
    return self;
}


-(NSString*) kind
{
    return [NSString stringWithUTF8String: SVN_AUTH_CRED_SIMPLE];
}

-(SFCCredentials*) credentialsForRealm: (NSString*) realm
                            parameters: (NSDictionary*) parameters
                              iterator: (id) iterator
                             firstTime: (bool) isFirstTime
                                 error: (NSError *__autoreleasing*) error
{
    NSString* userName = NSUserName();
    SFCSimpleCredentials* credentials;
    if (isFirstTime)
    {
        credentials = [[SFCSimpleCredentials alloc] initWithName: userName
                                                        password: _password
                                                         maySave: false];
    }
    else if (error != nil)
    {
		*error = [SFCError errorWithDomain: SVN_ERROR_DOMAIN
                                      code: SVN_ERR_AUTHN_CREDS_UNAVAILABLE
                      localizedDescription: @"No more chances!"];
    }
    return credentials;
}

@end